package com.zhihu_lvliangi_android.option.home;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.GridView;

import com.zhihu_lvliangi_android.R;
import com.zhihu_lvliangi_android.domain.CureRecord;
import com.zhihu_lvliangi_android.option.general.GeneralWebviewActivity;
import com.zhihu_lvliangi_android.option.home.adapter.zhengwuAdapter;
import com.zhihu_lvliangi_android.option.mycircle.parents.MyParententInfoActivity;
import com.zhihu_lvliangi_android.option.parent.ParentFragment;
import com.zhihu_lvliangi_android.option.yiliao.CureRecordActivity;
import com.zhihu_lvliangi_android.option.yiliao.HealthRecordActivity;
import com.zhihu_lvliangi_android.option.zhengwu.AffairDetailActivity;
import com.zhihu_lvliangi_android.option.zhengwu.AffairEmploymentActivity;
import com.zhihu_lvliangi_android.option.zhengwu.AffairGuidActivity;
import com.zhihu_lvliangi_android.option.zhengwu.GuideEvaluateActivity;
import com.zhihu_lvliangi_android.option.zhengwu.GuideProgressActivity;
import com.zhihu_lvliangi_android.option.zhengwu.NotificationActivity;
import com.zhihu_lvliangi_android.util.RequestURL;

public class HomepageDialogItemsFragment extends ParentFragment {
	public static final String Key = "HomepageDialogItemsFragment";
	private int mId;
	private GridView mGridView;



	public static HomepageDialogItemsFragment newInstance(int id) {
		HomepageDialogItemsFragment homepageDialogItemsFragment = new HomepageDialogItemsFragment();
		Bundle bundle = new Bundle();
		bundle.putInt(Key, id);
		homepageDialogItemsFragment.setArguments(bundle);
		return homepageDialogItemsFragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mId = getArguments().getInt(Key);
	}


	public int getCurrentID(){
		return mId;
	}

	@Override
	protected int getLayoutId() {
		return R.layout.homepage_dialog_items;
	}

	@Override
	protected void setupViews(View parentView) {
		zhengwuAdapter adapter = null;
		String [] titles = null;
		FrameLayout frameLayout = (FrameLayout) parentView.findViewById(R.id.main_dialog_frame);
		switch (getArguments().getInt(Key)) {
		case R.id.zhihui_zhengwu:
			frameLayout.setBackgroundResource(R.drawable.main_option_dialog);
			titles = getResources().getStringArray(R.array.zhihui_zhengwu_names);
			break;
		case R.id.zhihui_jiaotong:
			frameLayout.setBackgroundResource(R.drawable.main_option_dialog_midd);
			titles = getResources().getStringArray(R.array.zhihui_jiaotong_names);
			break;
		case R.id.zhihui_yiliao:
			frameLayout.setBackgroundResource(R.drawable.main_option_dialog_right);
			titles = getResources().getStringArray(R.array.zhihui_yiliao_names);
			break;
		case R.id.zhihui_jiaoyu:
			frameLayout.setBackgroundResource(R.drawable.main_option_dialog);
			titles = getResources().getStringArray(R.array.zhihui_jiaoyu_names);
			break;
		case R.id.zhihui_shequ:
			frameLayout.setBackgroundResource(R.drawable.main_option_dialog_midd);
			titles = getResources().getStringArray(R.array.zhihui_shequ_names);
			break;
		case R.id.my_circle:
			frameLayout.setBackgroundResource(R.drawable.main_option_dialog_right);
			titles = getResources().getStringArray(R.array.zhihui_circle_names);
			break;

		default:
			break;
		}
		mGridView= (GridView) parentView.findViewById(R.id.homepahe_dialog_items_gridview);
		adapter = new zhengwuAdapter(getActivity(), getResources().obtainTypedArray(R.array.zhihui_zhengwu_pngs),titles);
		mGridView .setAdapter(adapter);
		mGridView.setOnItemClickListener(listener);


	}

	private OnItemClickListener  listener =new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			switch (mId) {
			case R.id.zhihui_zhengwu:
				zhihui_zhengwu(position);
				break;
			case R.id.zhihui_jiaotong:
				zhihui_jiaotong(position);
				break;
			case R.id.zhihui_yiliao:
				zhihui_yiliao(position);
				break;
			case R.id.my_circle:
				my_circle(position);
				break;
			case R.id.zhihui_jiaoyu:
				zhihui_jiaoyu(position);
				break;
			case R.id.zhihui_shequ:
				zhihui_shequ(position);
				break;
			default:
				break;
			}

		}



		private void my_circle(int position) {
			switch (position) {
			case 2:
				startActivity(new Intent(getActivity(), MyParententInfoActivity.class));
				break;
			case 3://子女
				tend2Jiaoyu();
				break;
			default:
				break;
			}
		}



		private void zhihui_shequ(int position) {
			// TODO Auto-generated method stub
			Bundle bundle = new Bundle();
			switch (position) {
			case 0:
				bundle.putString(GeneralWebviewActivity.URL_KEY, RequestURL.getzhihui_shequ_financial());
				break;
			case 1:
				bundle.putString(GeneralWebviewActivity.URL_KEY, RequestURL.getzhihui_shequ_paycost());
				break;
			case 2:
				bundle.putString(GeneralWebviewActivity.URL_KEY, RequestURL.getzhihui_shequ_juweihui());
				break;
			case 3  :
				bundle.putString(GeneralWebviewActivity.URL_KEY, RequestURL.getzhihui_shequ_usedcar());
				break;
			default:
				return;
			}
			startActivity(GeneralWebviewActivity.class,bundle);

		}



		private void zhihui_yiliao(int position) {
			Bundle bundle = new Bundle();
			switch (position) {
			case 0://药品查询
				bundle.putString(GeneralWebviewActivity.URL_KEY, RequestURL.getDrugSearchUrl());
				break;
			case 1://药店查询
				bundle.putString(GeneralWebviewActivity.URL_KEY, RequestURL.getDrugStoreSearchUrl());
				break;
			case 2  ://预约挂号
				bundle.putString(GeneralWebviewActivity.URL_KEY, RequestURL.getPreAppointUrl());
				break;
			case 3  :
				startActivity(new Intent(getActivity(), CureRecordActivity.class));
				return;
			case 4  :
				startActivity(new Intent(getActivity(), HealthRecordActivity.class));
				return;
			default:
				return;
			}
			startActivity(GeneralWebviewActivity.class,bundle);
		}

		private void zhihui_zhengwu(int position) {
			Bundle bundle = new Bundle();
			switch (position) {
			case 0:
//				bundle.putString(GeneralWebviewActivity.URL_KEY, RequestURL.getMyRimUrl());
				startActivity(NotificationActivity.class);
				return ;
			case 1:
				startActivity(new Intent(getActivity(), AffairGuidActivity.class));
//				startActivity(new Intent(getActivity(), AffairDetailActivity.class));
				return;
			case 2  :
				startActivity(new Intent(getActivity(), GuideProgressActivity.class));
				return;
			case 3  :
				startActivity(new Intent(getActivity(), AffairEmploymentActivity.class));
				return;
			case 4://预约代驾
				startActivity(new Intent(getActivity(), GuideEvaluateActivity.class));
				return;

			default:
				break;
			}
			startActivity(GeneralWebviewActivity.class,bundle);
		}


	};

	private void zhihui_jiaotong(int position) {
		Bundle bundle = new Bundle();
		switch (position) {
		case 0:
			bundle.putString(GeneralWebviewActivity.URL_KEY, RequestURL.getMyRimUrl());
			break;
		case 1:
			bundle.putString(GeneralWebviewActivity.URL_KEY, RequestURL.getBusRideUrl());
			break;
		case 2:
			bundle.putString(GeneralWebviewActivity.URL_KEY, RequestURL.getVehicleRecordUrl());
			break;
		case 3  :
			bundle.putString(GeneralWebviewActivity.URL_KEY, RequestURL.getPayCostUrl());
			break;
		case 4  :
			bundle.putString(GeneralWebviewActivity.URL_KEY, RequestURL.getRoadInfoUrl());
			break;
		case 5  :
			bundle.putString(GeneralWebviewActivity.URL_KEY, RequestURL.getCarPartUrl());
			break;
		case 6://预约代驾
			bundle.putString(GeneralWebviewActivity.URL_KEY, RequestURL.getYuYueDaiJiaUrl());
			break;

		default:
			return;
		}
		startActivity(GeneralWebviewActivity.class,bundle);
	}
	private void zhihui_jiaoyu(int position) {
		// TODO Auto-generated method stub
		switch (position) {
		case 0://儿童手表
			tend2Jiaoyu();
			break;
		default:
			return;
		}
	}

	private void tend2Jiaoyu() {
		ComponentName componetName = new ComponentName(
				//这个是另外一个应用程序的包名
				"com.example.kidproject",
				//这个参数是要启动的Activity
				"com.example.kidproject.Pt30NewWelcomeActivity");
		try {
			Intent intent = new Intent();
			Bundle bundle1 = new Bundle();
			intent.putExtras(bundle1);
			intent.setComponent(componetName);
			startActivity(intent);
		} catch (Exception e) {
			//如果没找到 可以给出提示 或者其他操作
			//    通过AlertDialog.Builder这个类来实例化我们的一个AlertDialog的对象
			AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
			//    设置Title的图标
			builder.setIcon(R.drawable.ic_launcher);
			//    设置Title的内容
			builder.setTitle("提示");
			//    设置Content来显示一个信息
			builder.setMessage("您尚无安装儿童智能手表客户端，是否安装？");

			//    设置一个PositiveButton
			builder.setPositiveButton("确定", new DialogInterface.OnClickListener()
			{
				@Override
				public void onClick(DialogInterface dialog, int which)
				{
//						Bundle bundle = new Bundle();
//						bundle.putString(GeneralWebviewActivity.URL_KEY, RequestURL.getzhihui_jiaoyu_download_Url());
//						startActivity(GeneralWebviewActivity.class,bundle);
					Intent intent = new Intent();
			        intent.setAction("android.intent.action.VIEW");
			        Uri content_url = Uri.parse(RequestURL.getzhihui_jiaoyu_download_Url());
			        intent.setData(content_url);
			        startActivity(intent);

				}
			});
			//    设置一个NegativeButton
			builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
			{
				@Override
				public void onClick(DialogInterface dialog, int which)
				{
					//	                	  builder.show();
				}
			});
			//    显示出该对话框
			builder.show();
		}
	}
	@Override
	protected void initialized(Bundle savedInstanceState) {

	}

	@Override
	public void threadTask() {

	}

}
